﻿using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace StockAnomalyDetector
{
    public class Foo
    {
        private ProgressBar bar;
        private ProgressBar bar2;
        

        public Foo(ProgressBar bar, ProgressBar bar2)
        {
            this.bar = bar;
            this.bar2 = bar2;
        }
        

        private ProgressReporter progressReporter;

        private int foo()
        {
            for (int i = 0; i != 50; ++i)
            {
                Thread.Sleep(200);
                progressReporter.ReportProgress(() =>
                {
                    bar.Value += 1; // 双线程都会发挥作用
                });
            }
            return 42;
        }

        public void StartBackgroundTask()
        {
            progressReporter = new ProgressReporter();
            var task = Task.Factory.StartNew(() =>
            {
                foo();
            });
            var task2 = Task.Factory.StartNew(() =>
            {
                //foo();
            });

            // ProgressReporter can be used to report successful completion, 
            //  cancelation, or failure to the UI thread. 
            //progressReporter.RegisterContinuation(task, () =>
            //{
            //    // Update UI to reflect completion. 
            //    this.bar.Value = 100;
            //    this.bar2.Value = 100;

            //    // Display results. 
            //    if (task.Exception != null)
            //        MessageBox.Show("Background task error: " + task.Exception.ToString());
            //    else if (task.IsCanceled)
            //        MessageBox.Show("Background task cancelled");
            //});
            


        }

    }
}